package com.chengyu.core.controller.shop;

import com.alibaba.fastjson.JSONArray;
import com.chengyu.core.component.OperationLog;

import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.form.OrderDeliveryForm;
import com.chengyu.core.domain.form.OrderSearchForm;
import com.chengyu.core.domain.result.OrderFreightResult;
import com.chengyu.core.domain.result.OrderResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.OmsOrder;
import com.chengyu.core.model.OmsOrderPriceLog;
import com.chengyu.core.model.UmsShop;
import com.chengyu.core.model.UmsShopAccount;
import com.chengyu.core.service.order.OrderFreightService;
import com.chengyu.core.service.order.OrderPriceLogService;
import com.chengyu.core.service.order.OrderService;
import com.chengyu.core.service.order.OrderWriteOffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @title  订单
 * @author LeGreen
 * @date   2022/5/7
 */
@Api(tags = "订单")
@Controller
@RequestMapping("/admin/order")
public class AdminOrderController extends UserBaseController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderFreightService orderFreightService;
	@Autowired
	private OrderPriceLogService orderPriceLogService;


	@Autowired
	private OrderWriteOffService writeOffService;


	@ApiOperation(value = "订单列表")
	@ResponseBody
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public CommonResult<CommonPage<OrderResult>> getList(OrderSearchForm form,
													  @RequestParam(value = "page", defaultValue = "1") Integer page,
													  @RequestParam(value = "limit", defaultValue = "20") Integer pageSize) throws ServiceException {
		form.setShopId(getCurrentShop().getId());
		CommonPage<OrderResult> list = orderService.getOrderList(form, page, pageSize);
		return CommonResult.success(list);
	}

	@ApiOperation(value = "订单备注")
	@ResponseBody
	@RequestMapping(value="/mark", method=RequestMethod.POST)
//	public CommonResult<CommonPage<OrderResult>> mark( String orderNo, String remark) throws ServiceException {
	public CommonResult<CommonPage<OrderResult>> mark(@RequestBody Map<String,String> map, HttpServletRequest request) throws ServiceException {



		String orderNo =  map.get("orderNo");
		String remark =  map.get("remark");


		UmsShop umsShop = getCurrentShop();
		OmsOrder omsOrder =  orderService.getOrderByOrderNoShopId(orderNo,umsShop.getId());
		if(omsOrder==null){
			return CommonResult.failed( "订单不存在,数据错误!");
		}

		orderService.updateOrderRemark(omsOrder.getId(), remark);
		return CommonResult.success(null);
	}

	@OperationLog
	@ApiOperation(value = "订单核销")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "verifyCode", value = "核销码")
	})
	@ResponseBody
	@RequestMapping(value="/writeOff", method=RequestMethod.POST)
	public CommonResult<String> writeOff( @RequestBody Map<String,String> map, HttpServletRequest request) throws ServiceException {

		String orderNo =  map.get("orderNo");
		String verifyCode =  map.get("verifyCode");



		UmsShop umsShop = getCurrentShop();
		OmsOrder omsOrder =  orderService.getOrderByOrderNoShopId(orderNo,umsShop.getId());
		if(omsOrder==null){
			return CommonResult.failed( "订单不存在,数据错误!");
		}

		UmsShopAccount shopAccount  = shopAccountService.getAccountById(umsShop.getId());

		writeOffService.writeOff(shopAccount, verifyCode);
		return CommonResult.success(null);
	}


	@ApiOperation(value = "订单详情")
	@ResponseBody
	@RequestMapping(value="/get", method=RequestMethod.GET)
	public CommonResult<OrderResult> get(Integer orderId) throws ServiceException {
		OrderResult result = orderService.getOrder(orderId);
		return CommonResult.success(result);
	}

	@OperationLog
	@ApiOperation(value = "发货")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "deliveryJson", value = "[deliveryType:快递类型,orderId:订单ID,deliveryNo:快递单号]")
	})
	@ResponseBody
	@RequestMapping(value="/delivery", method=RequestMethod.POST)
	public CommonResult<String> delivery(@RequestBody OrderDeliveryForm orderDeliveryForm) throws ServiceException {

		List<OrderDeliveryForm> orderDeliveryForms = new ArrayList<OrderDeliveryForm>();
		orderDeliveryForms.add(orderDeliveryForm);
		String massage = orderService.delivery(getCurrentShop(), orderDeliveryForms);
		return CommonResult.success(massage);
	}

	@OperationLog
	@ApiOperation(value = "修改价格")
	@ResponseBody
	@RequestMapping(value="/editPriceSubmit", method=RequestMethod.POST)
	public CommonResult<String> editPriceSubmit(Integer id, BigDecimal editPrice) throws ServiceException {
		orderPriceLogService.updateOrderPrice(getCurrentShop(), id, editPrice);
		return CommonResult.success(null);
	}

	@ApiOperation(value = "物流详情")
	@ResponseBody
	@RequestMapping(value="/getFreightList", method=RequestMethod.GET)
	public CommonResult<OrderFreightResult> getDeliveryList(Integer orderId) {
		OrderFreightResult result = orderFreightService.getOrderFreight(orderId);
		return CommonResult.success(result);
	}

	@ApiOperation(value = "价格修改记录")
	@ResponseBody
	@RequestMapping(value="/getPriceLog", method=RequestMethod.GET)
	public CommonResult<List<OmsOrderPriceLog>> getPriceLog(Integer orderId) {
		List<OmsOrderPriceLog> list= orderPriceLogService.getOrderPriceLogByOrderId(orderId);
		return CommonResult.success(list);
	}



}